﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string input = txtInput.Text.Trim();
            if (string.IsNullOrEmpty(input) || input.Length != 1)
            {
                MessageBox.Show("请输入一个字符！");
                return;
            }
            char ch = input[0];
            if (!((ch >= '1' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')))
            {
                MessageBox.Show("请输入数字（1 - 9）或字母！");
                return;
            }
            lblTriangle.Text = "";
            int num;
            if (ch >= '1' && ch <= '9')
            {
                num = ch - '0';
            }
            else if (ch >= 'A' && ch <= 'Z')
            {
                num = ch - 'A' + 1;
            }
            else
            {
                num = ch - 'a' + 1;
            }
            // 先添加一行空格，使三角形更居中
            lblTriangle.Text += Environment.NewLine;
            for (int i = 1; i <= num; i++)
            {
                // 每行前添加若干空格，模拟居中效果（可根据需求调整空格数）
                lblTriangle.Text += new string(' ', num - i);
                lblTriangle.Text += new string(ch, 2 * i - 1) + Environment.NewLine;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInput.Text = "";
            lblTriangle.Text = "";
        }
    }
}
